<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="isDialog"
      width="500px"
      @close="onClose"
    >
      <el-form ref="form" :model="formData" label-width="80px" :rules="rules">
        <el-form-item label="所属小区" prop="community_code">
          <el-select v-model="formData.community_code" placeholder="请选择活动区域">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input
            v-model="formData.name"
            :rows="3"
            placeholder="请输入"
          />
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button
            type="primary"
            size="small"
            @click="onSubmit"
          >提交</el-button>
          <el-button size="small" @click="isDialog = false">关闭</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { getAddExpensesApi, getEditExpensesApi } from '@/api/expenses'
export default {
  name: 'VueAdminTemplateMasterAddForm',
  props: {
    options: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      isDialog: false,
      formData: {
        community_code: '',
        name: ''
      },
      value: '',
      rules: {
        community_code: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },

  mounted() {

  },
  methods: {
    // 增加按钮
    async   onSubmit() {
      await this.$refs.form.validate()
      this.formData.id ? await getEditExpensesApi(this.formData) : await getAddExpensesApi(this.formData)
      // await getAddExpensesApi(this.formData)
      this.$message.success(this.formData.id ? '修改成功' : '添加成功')
      this.$parent.getList()
      this.$message.success('添加成功')
      this.isDialog = false
    // 弹框关闭
    },
    onClose() {
      // 表单重置
      this.formData = {
        community_code: '',
        name: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
